<script type="text/html" template>
<div class="layui-fluid" >
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="editForm">
                <input type="hidden" id="userId" name="id"  value="{{d.id}}" />
                <div class="layui-form-item">
                    <label class="layui-form-label">用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="username" readonly value="{{d.username}}" autocomplete="off" placeholder="请输入用户名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>原密码</label>
                    <div class="layui-input-block">
                        <input type="text" name="password" lay-verify="required" value=""  placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>新密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="newPassword"  lay-verify="required|password"  value=""  placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label"><span style="color: red">*</span>确认密码</label>
                    <div class="layui-input-block">
                        <input type="password"  lay-verify="required|confirmPass" value=""  placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <a class="layui-btn" lay-submit="" lay-filter="editUserSubmit">立即提交</a>
                        <a class="layui-btn layui-btn-primary" id="close">关闭</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</script>
<script>
    var UpdatePassword = function (admin,form,layer, index,$) {
        //初始化
        this.init = function () {

            // 校验两次密码是否一致
            form.verify({
                confirmPass:function(value){
                    if($('input[name=newPassword]').val() !== value)
                        return '两次密码输入不一致！';
                },
                password:function (value) {
                    if($('input[name=password]').val() == value){
                        return '新密码不能和原密码相同！';
                    }
                    var patrn=/^(\w){6,20}$/;
                    if(!patrn.exec(value)){
                        return '密码只能输入6-20个字母、数字、下划线！';
                    }
                }
            });
            /* 监听提交 */
            form.on('submit(editUserSubmit)', function (data) {
                var requestData = data.field;
                //提交用户
                admin.post({
                    api:'updatePassword',
                    data: JSON.stringify(requestData),
                    contentType: 'application/json;charset=UTF-8',
                    success:function (res) {
                        if(res.code == 0){
                            admin.success('操作成功',function () {
                                parent.layer.close(index);
                            });
                        }
                    }
                });
                return false;
            });
            //关闭窗口
            $("#close").click(function () {
                parent.layer.close(index);
            });
        };
    };

</script>